'''
Created on Aug 30, 2012

@author: kingsfield
'''
import os, sys
_root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if not _root_dir in sys.path:
    sys.path.insert(0, _root_dir)
sys.path.insert(0, '/home/kingsfield/software/liblinear-1.91/python')

from util import Util, DataLoader
from datetime import datetime
import time
import multiprocessing
from util import Constant

wordlen = Constant.wordlen
catlen = Constant.catlen
daylen = Constant.daylen
hourlen = Constant.hourlen

__mydebug__ = False

class Classfier(object):
    '''
    classdocs
    '''
    
    DataLoader.Idmap.load()
    cat_item_count = DataLoader.cat_item_count
    
    bayes_cat_item_count = dict()
    bayes_cat_word_item_count = dict()
    bayes_cat_count = dict()
    
    bayes_cat_item_prior = dict()
    bayes_cat_word_item_likehood = dict()
    
    def __init__(self, params):
        '''
        Constructor
        '''
        
    
    @classmethod
    def train(cls):
        filepath = '/home/kingsfield/Desktop/kaggle/kaggle/train.csv'
        sum_line = 1865270
        with open(filepath) as fr:
            line = fr.readline()
            line_count = 1
            step = sum_line / 100
            while True:
                line_count += 1
                if __mydebug__:
                    if line_count / step >= 5:
                        break
                if line_count % step == 0:
                    print '--%d' % (line_count / step)
                line = fr.readline()
                if not line:
                    break
                __uid, iid, cat, query, __clickt, __queryt = Util.train_parse(line)
                words = Util.wordutil.getwords(query)
                if cat not in cls.bayes_cat_item_count:
                    cls.bayes_cat_item_count[cat] = dict()
                cls.bayes_cat_item_count[cat][iid] = cls.bayes_cat_item_count[cat].get(iid, 0) + 1
                if cat not in cls.bayes_cat_word_item_count:
                    cls.bayes_cat_word_item_count[cat] = dict()
                if iid not in cls.bayes_cat_word_item_count[cat]:
                    cls.bayes_cat_word_item_count[cat][iid] = dict()
                for w in words:
                    if w not in cls.bayes_cat_word_item_count[cat][iid]:
                        cls.bayes_cat_word_item_count[cat][iid][w] = dict()
                    cls.bayes_cat_word_item_count[cat][iid][w] = cls.bayes_cat_word_item_count[cat][iid].get(w, 0) + 1
        
        for cat in cls.bayes_cat_item_count:
            line = cls.bayes_cat_item_count[cat].item()
            sum_count = sum([i[1] for i in line])
            cls.bayes_cat_count[cat] = sum_count()
            cls.bayes_cat_item_prior[cat] = dict()
            cls.bayes_cat_word_item_likehood[cat] = dict()
            for iid in cls.bayes_cat_item_count[cat]:
                'TODO' 
                cls.bayes_cat_item_prior[cat][iid] = cls.bayes_cat_item_count[cat][iid] * 1.0 / sum_count
                cls.bayes_cat_word_item_count[cat][iid] = dict()
                item_count = cls.bayes_cat_item_count[cat][iid]
                for w in cls.bayes_cat_word_item_count[cat][iid]:
                    'TODO'
                    cls.bayes_cat_word_item_likehood[cat][iid][w] = cls.bayes_cat_word_item_count[cat][iid][w] * 1.0 / item_count 
                    
                       
            
    @classmethod
    def _predict(cls, query, cat, alpha, beta, defalut_likehood):
        words = Util.wordutil.getwords(query)
        for iid in cls.bayes_cat_item_likehood[cat]:
            ''
                
                
                 
